package clustering;
import training_set.*;
import preprocess.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class SeedCluster_Identity {
	private String Mesh_phrases[] = {"common cold", "cold temperature", "cold-shock response", "Cold Shock Proteins and Peptides",
										"Extreme Cold", "Cold Ischemia", "Cold Climate"};
	
	public SeedCluster_Identity(String positive_file, String base_file){
		List<String> positive_pairs = GetPairs_Id(positive_file);
		List<String> base_pairs = GetPairs_Id(base_file);
		BuildHashMap(positive_pairs,positive_pair_ids);
		BuildHashMap(base_pairs, base_pair_ids);
	}
	
	public boolean Contain_Mesh_Phrase(String line){
		for(int i=0; i<Mesh_phrases.length; i++)
			if(line.toLowerCase().contains(Mesh_phrases[i].toLowerCase()))
				return true;
		return false;
	}
	
	public int Contain_Same_Mesh_Phrase(String line1, String line2){
		for(int i=0; i<Mesh_phrases.length; i++)
			if(line1.toLowerCase().contains(Mesh_phrases[i].toLowerCase()) && line2.toLowerCase().contains(Mesh_phrases[i].toLowerCase()))
				return 1;
		return 0;
	}
	
	public List<String> GetLocalforCold(String line){
		if(line == null || line.length()==0)
			return null;
		List<String> locals = new ArrayList<String>();
		String cold_sentence = new One_Instance_Filter().GetColdSentence(line);
		Text_Clean cleanser = new Text_Clean();
		cold_sentence = cleanser.cleanSentence(cold_sentence);
		String tokens[] = cold_sentence.split(" ");
		for(int i=0; i<tokens.length; i++){
			if(tokens[i].contains("cold")){
				StringBuffer local = new StringBuffer();
				if(i-2>=0)	local.append(tokens[i-2]+" ");
				if(i-1>=0)	local.append(tokens[i-1]+" ");
				local.append("cold");
				if(i+1<tokens.length) local.append(" "+ tokens[i+1]);
				if(i+2<tokens.length) local.append(" "+tokens[i+2]);
				locals.add(local.toString().trim());
			}
		}
		
		return locals;
	}
	
	public int Contain_Same_Ngrams(List<String> grams1, List<String> grams2){
		if(grams1 == null || grams1.size()==0)
			return 0;
		if(grams2 == null || grams2.size()==0)
			return 0;
		if(grams1.size() != grams2.size())
			return 0;
		List<String> temp = new ArrayList<String>(grams1);
		for(int i=0; i<grams2.size(); i++)
			for(int j=0; j<temp.size(); j++){
				if(grams2.get(i).equalsIgnoreCase(temp.get(j))){
					temp.remove(j);
					break;
				}
			}
		if(temp.size()==0)
			return 1;
		return 0;
	}
	
	private HashMap<String,Integer> positive_pair_ids = new HashMap<String,Integer>();
	private HashMap<String,Integer> base_pair_ids = new HashMap<String,Integer>();
	
	public void BuildHashMap(List<String> pairs, HashMap<String,Integer> pair_hash){
		for(String pair: pairs){
			pair_hash.put(pair, pair_hash.size());
		}
	}
	public List<String> GetPairs_Id(String filename){
		List<String> pairs_id = new ArrayList<String>();
		File f = new File(filename);
		FileInputStream fis;
		try{
			fis = new FileInputStream(f);
			InputStreamReader isr=new InputStreamReader(fis);
			BufferedReader br=new BufferedReader(isr);
			String line = br.readLine();
			while(line != null){
				line = line.trim();
				if(line.length()>0){
					pairs_id.add(line);
				}
				line = br.readLine();
			}
			br.close();
		}catch(IOException e)
		{
			e.printStackTrace();
		}	
		return pairs_id;
	}
	public int IsPair_from_Training(String id1, String id2){
		String key1 = id1+"\t"+id2;
		String key2 = id2+"\t"+id1;
		if(positive_pair_ids.containsKey(key1) || positive_pair_ids.containsKey(key2))
			return 1;
		if(base_pair_ids.containsKey(key1)|| base_pair_ids.containsKey(key2))
			return -1;
		return 0;
	}
	
	public int Get_Score_from_Seeds(boolean usingMesh, boolean usingNgrams, boolean usingTraining, Article a, Article b){
		int score = 0;
		if(usingMesh)
			score += Contain_Same_Mesh_Phrase(a.getTiab(),b.getTiab());
		if(usingNgrams){
			int bigram_score = Contain_Same_Ngrams(a.getCold_bigrams(), b.getCold_bigrams());
			int trigram_score = Contain_Same_Ngrams(a.getCold_trigrams(),b.getCold_trigrams());
			score += bigram_score*trigram_score;
		}
		if(usingTraining){
			
			int training_score = IsPair_from_Training(a.getPmid(),b.getPmid());
			if(training_score == -1)
				return 0;
			score += training_score;
		}
		return score;
	}
}
